﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MadeInPortugal;
using MadeInPortugal.Class;

namespace MadeInPortugalWebApplication.WebControl
{
    public partial class Navigation : System.Web.UI.UserControl
    {
        int UserId = 0;
        String UserType;

        protected void Page_Load(object sender, EventArgs e)
        {
            if(Session["Acordeon"]==null)
            {
                if (!IsPostBack)
                {
                    CheckUser();

                    ProductType ProdType = new ProductType();
                    ProductType[] type = ProdType.GetAllType();
                    foreach (ProductType item in type)
                    {
                        accordion.InnerHtml += "<h3><a href='#'>" + item.Type + " </a></h3>";
                        Tag tag = new Tag();
                        Tag[] tagList = tag.GetTagByTypeId(item.ID);
                        accordion.InnerHtml += "<div>";
                        if (tagList.Length != 0)
                        {

                            foreach (Tag tags in tagList)
                            {
                                if (UserType == "Admin")
                                    accordion.InnerHtml += "<p><a href='AdminProductList.aspx?Id=" + tags.ID + "'>" + tags.TagName + "</a></p>";
                                else
                                    accordion.InnerHtml += "<p><a href='index.aspx?Id=" + tags.ID + "'>" + tags.TagName + "</a></p>";

                            }
                            // if (accordion.InnerHtml.Substring(accordion.InnerHtml.Length - 4, 4) == "</p>")

                        }
                        accordion.InnerHtml += "</div>";

                    }
                    Session["Acordeon"] = accordion.InnerHtml;
                }
            }
            else
            {
                accordion.InnerHtml = Session["Acordeon"].ToString();
            }

        }
        private void CheckUser()
        {
            if (Session["UserType"] != null)
                UserType = Session["UserType"].ToString();
            if (Session["UserId"] != null)
                int.TryParse(Session["UserId"].ToString(), out UserId);
            
            
        }
    }
}